<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ModifyKkValue extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'modify_kk_value';
    public $timestamps = false;



    public function getList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $where['room_id'] = $request->input('room_id');
        $where['month'] = $request->input('month');
        $data =  DB::connection($this->connection)->table('modify_kk_value as f')
            ->select('f.*')
            ->where($where)->get();
        foreach($data as &$v){
            $v->original = $v->original / 100;
            $v->modify = $v->modify / 100;
        }
        $total = count($data);
        $res['data'] = collect($data)->forPage($page, $limit)->values()->toArray();
        $res['total'] = $total;
        return $res;
    }


    public function createLog(Request $request , $user)
    {
        //修改接口
        $curlData['id'] = $request->input('id');
        $curlData['modify'] = $request->input('modify') * 100;
        $res =  curl_request(getKkaudioApiUrl() . '/inner/finance/room/wealth_month/update',
            $curlData, 'urlencoded', 'GET');
        if ($res['code'] !== 0) {
           return $res;
        }
        $data['room_id'] = $request->input('room_id');
        $data['admin_id'] = $user['id'];
        $data['admin_name'] = $user['name'];
        $data['original'] = $request->input('original')  * 100;
        $data['modify'] = $request->input('modify') * 100;
        $data['desc'] = $request->input('desc');
        $data['month'] = $request->input('month');
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s',time());
        OperationLogs::createData($request,OperationLogs::BANNER,'修改金币', $data['room_id']);

        return DB::connection($this->connection)->table($this->table)->insert($data);
    }



}
